package com.app.zenmewan.controller;

import com.app.zenmewan.commons.Message;
import com.app.zenmewan.entity.TempArticle;
import com.app.zenmewan.commons.Actions;
import com.app.zenmewan.entity.User;
import com.app.zenmewan.service.TempArticleService;
import com.app.zenmewan.service.UserService;
import org.omg.CORBA.MARSHAL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import javax.annotation.Resource;
import java.util.List;

/**
 * (TempArticle)表控制层
 *
 * @author makejava
 * @since 2020-05-02 19:58:24
 */
@RestController
@RequestMapping("tempArticle")
public class TempArticleController {
    /**
     * 服务对象
     */
    @Resource
    private TempArticleService tempArticleService;
    @Resource
    private UserService userService;
    @Autowired
    private Actions actions;
    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public TempArticle selectOne(Integer id) {
        return this.tempArticleService.queryById(id);
    }

    @PostMapping("save/{token}")
    public ModelAndView save(@RequestBody TempArticle t,@PathVariable("token") String token){
        ModelAndView modelAndView = new ModelAndView(new MappingJackson2JsonView());
        int result = actions.checkToken(token,modelAndView);
        if(result==-1){
            return modelAndView;
        }else{
            //存在该用户token
            try{
                t.setUid(result);
                TempArticle findobj = new TempArticle();
                findobj.setUid(result);
                List<TempArticle> list = this.tempArticleService.queryAll(findobj);
                //如果有存在的话，则更新
                if (list.size()>0){
                    t.setId(result);
                    this.tempArticleService.update(t);
                }else{
                    //否则新建一条数据
                    this.tempArticleService.insert(t);
                }
                modelAndView.setStatus(HttpStatus.resolve(200));
                return modelAndView;
            }catch (Exception e){
                modelAndView.setStatus(HttpStatus.BAD_REQUEST);
                modelAndView.addObject(Message.MESS,"意外出错:"+e.getLocalizedMessage());
                return modelAndView;
            }
        }
    }
    @PostMapping("init/{token}")
    public ModelAndView init(@PathVariable("token") String token){
        ModelAndView modelAndView = new ModelAndView(new MappingJackson2JsonView());
        int result = actions.checkToken(token,modelAndView);
        if(result==-1){
            return modelAndView;
        }else{
            TempArticle findobj = new TempArticle();
            findobj.setUid(result);
            List<TempArticle> list = this.tempArticleService.queryAll(findobj);
            //如果有存在的话，则更新
            if (list.size()>0){
                modelAndView.addObject("article",list.get(0));
            }else{
                modelAndView.addObject("article",null);
            }
            return modelAndView;
        }
    }
}